package com.jt.config;

import org.apache.catalina.Host;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Configuration //标识我是配置类
@PropertySource("classpath:/properties/redis.properties")
public class RedisConfig {

    @Value("${redis.nodes}")
    private String redisNodes;  //node,node,node

    @Bean
    public JedisCluster jedisCluster(){
        Set<HostAndPort> nodeSet = new HashSet<>();
        String[] clusters = redisNodes.split(",");
        for(String cluster:clusters){
            String host = cluster.split(":")[0];
            int port = Integer.parseInt(cluster.split(":")[1]);
            nodeSet.add(new HostAndPort(host,port));
        }
        return new JedisCluster(nodeSet);
    }


//    @Value("${redis.nodes}")
//    private String nodes;  //node,node,node

//    /**
//     *  spring整合redis分片机制
//     */
//    @Bean
//    public ShardedJedis shardedJedis() {
//        //1.获取每个节点的信息
//        String[] strNodes = nodes.split(",");
//        List<JedisShardInfo> shards = new ArrayList<>();
//        //2.遍历所有node，为list集合赋值
//        for(String node : strNodes){ //node=ip:port
//            String host = node.split(":")[0];
//            int port = Integer.parseInt(node.split(":")[1]);
//            JedisShardInfo info = new JedisShardInfo(host,port);
//            shards.add(info);
//        }
//        ShardedJedis shardedJedis = new ShardedJedis(shards);
//        return shardedJedis;
//    }

//    @Value("${redis.host}")
//    private String host;
//    @Value("${redis.port}")
//    private Integer port;
//
//    @Bean
//    public Jedis jedis(){
//        //数据写死？？？？？
//        return new Jedis("192.168.126.129",6379);
//    }
}
